﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Model;

namespace BO
{
    /// <summary>
    /// Summary description for VYC00BO
    /// </summary>
    public class VYC00BO : DBConnectionBO
    {
        public VYC00BO()
            : base()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        /// <summary>
        /// Lay danh sach cac du lieu trong VYC00
        /// </summary>
        /// <returns></returns>
        public List<ModelVYC00> GetAllList()
        {
            var list = new List<ModelVYC00>();
            const string query = "SELECT [Id],[Ten],[MoTa],[NgayCapNhat],[TrangThai] FROM [VYC-00]";
            var sqlDataAdapter = new SqlDataAdapter(query, connection);
            var dataTable = new DataTable();
            sqlDataAdapter.Fill(dataTable); // fill data to Datatable
            CloseConnection(); // Close connection to database
            for (var i = 0; i < dataTable.Rows.Count; i++)
            {
                var model = new ModelVYC00();

                model.Id = (dataTable.Rows[i][0] == DBNull.Value) ? string.Empty : dataTable.Rows[i][0].ToString();
                model.Ten = (dataTable.Rows[i][1] == DBNull.Value) ? string.Empty : dataTable.Rows[i][1].ToString();
                model.MoTa = (dataTable.Rows[i][2] == DBNull.Value) ? string.Empty : dataTable.Rows[i][2].ToString();
                model.NgayCapNhat = (dataTable.Rows[i][3] == DBNull.Value) ? DateTime.Now : (DateTime)dataTable.Rows[i][3];
                model.TrangThai = (dataTable.Rows[i][4] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][4]);

                list.Add(model);
            }
            return list;
        }

        /// <summary>
        /// Add new a model in data
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddData(ModelVYC00 model)
        {
            var query = "INSERT INTO [VYC-00]([Id],[Ten],[MoTa],[NgayCapNhat],[TrangThai]) VALUES (" +
                        " '" + model.Id + "', " +
                        "N'" + model.Ten + "', " +
                        "N'" + model.MoTa + "', " +
                        "GETDATE(), " + model.TrangThai + ")";
            var sqlCommand = new SqlCommand(query, connection);
            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
                throw;
            }
        }

        /// <summary>
        /// Edit a model in data
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int EditData(ModelVYC00 model)
        {
            var query = "UPDATE [VYC-00] SET " +
                        "Ten = '" + model.Ten + "', " +
                        "MoTa = N'" + model.MoTa + "', " +
                        "NgayCapNhat = GETDATE(), TrangThai = " + model.TrangThai +
                        " WHERE Id like '" + model.Id + "'";
            var sqlCommand = new SqlCommand(query, connection);
            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
                throw;
            }
        }

        /// <summary>
        /// Delete a model in data
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int DeleteData(ModelVYC00 model)
        {
            var query = "DELETE FROM [VYC-00] " +
                        " WHERE Id like '" + model.Id + "'";
            var sqlCommand = new SqlCommand(query, connection);
            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
                throw;
            }
        }

        /// <summary>
        /// Get by id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<ModelVYC00> GetById(string id)
        {
            var list = new List<ModelVYC00>();
            var query = "SELECT [Id],[Ten],[MoTa],[NgayCapNhat],[TrangThai] FROM [VYC-00] WHERE Id like '" + id + "'";
            var sqlDataAdapter = new SqlDataAdapter(query, connection);
            var dataTable = new DataTable();
            sqlDataAdapter.Fill(dataTable); // fill data to Datatable
            CloseConnection(); // Close connection to database
            for (var i = 0; i < dataTable.Rows.Count; i++)
            {
                var model = new ModelVYC00();

                model.Id = (dataTable.Rows[i][0] == DBNull.Value) ? string.Empty : dataTable.Rows[i][0].ToString();
                model.Ten = (dataTable.Rows[i][1] == DBNull.Value) ? string.Empty : dataTable.Rows[i][1].ToString();
                model.MoTa = (dataTable.Rows[i][2] == DBNull.Value) ? string.Empty : dataTable.Rows[i][2].ToString();
                model.NgayCapNhat = (dataTable.Rows[i][3] == DBNull.Value) ? DateTime.Now : (DateTime)dataTable.Rows[i][3];
                model.TrangThai = (dataTable.Rows[i][4] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][4]);

                list.Add(model);
            }
            return list;
        }

    }
}